﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UtilZ.Dotnet.Ex.Base;
using UtilZ.Dotnet.Ex.Log;

namespace UtilZ.Tool.WatchdogPlugin
{
    public partial class AppItem : IDisposableZ
    {
        #region 属性字段

        private string _appName = null;
        /// <summary>
        /// 应用程序名称
        /// </summary>
        public string AppName
        {
            get { return this._appName; }
            set { this._appName = value; }
        }

        public string OldAppName { get; set; }

        private string _processName = null;
        private string _exeFilePath = string.Empty;

        private string _appExeFilePath = string.Empty;
        /// <summary>
        /// 启动文件路径
        /// </summary>
        public string AppExeFilePath
        {
            get { return this._appExeFilePath; }
            set
            {
                if (string.IsNullOrWhiteSpace(value))
                {
                    this._appExeFilePath = null;
                }
                else
                {
                    this._appExeFilePath = value.Trim();
                }
            }
        }




        private int _processId = 0;
        /// <summary>
        /// 进程ID
        /// </summary>
        public int ProcessId
        {
            get { return this._processId; }
            set { this._processId = value; }
        }

        private bool _runStatus = false;
        /// <summary>
        /// 运行状态[ture:运行中;false:已停止]
        /// </summary>
        public bool RunStatus
        {
            get { return this._runStatus; }
            set { this._runStatus = value; }
        }

        private DateTime? _startTime = null;
        /// <summary>
        /// 启动时间
        /// </summary>
        public DateTime? StartTime
        {
            get { return this._startTime; }
            set { this._startTime = value; }
        }

        private int _CPURate = 0;
        /// <summary>
        /// CPU使用率
        /// </summary>
        public int CPURate
        {
            get { return this._CPURate; }
            set { this._CPURate = value; }
        }

        private long _memUse = 0L;
        /// <summary>
        /// 获取内存使用大小,单位:Byte
        /// </summary>
        public long MemUse
        {
            get { return this._memUse; }
            set { this._memUse = value; }
        }

        private int _thrNum = 0;
        /// <summary>
        /// 获取当前应用线程数
        /// </summary>
        public int ThrNum
        {
            get { return this._thrNum; }
            set { this._thrNum = value; }
        }

        //private string _tcpPortUse = null;
        ///// <summary>
        ///// TCP端口使用列表';'分隔
        ///// </summary>
        //public string TcpPortUse
        //{
        //    get { return this._tcpPortUse; }
        //    set { this._tcpPortUse = value; }
        //}

        //private string _udpPortUse = null;
        ///// <summary>
        ///// UDP端口使用列表';'分隔
        ///// </summary>
        //public string UdpPortUse
        //{
        //    get { return this._udpPortUse; }
        //    set { this._udpPortUse = value; }
        //}






        private string _softwVer = null;
        /// <summary>
        /// 软件版本信息
        /// </summary>
        public string SoftwVer
        {
            get { return this._softwVer; }
            set { this._softwVer = value; }
        }

        private string _des = null;
        /// <summary>
        /// 备注/描述/说明
        /// </summary>
        public string Des
        {
            get { return this._des; }
            set { this._des = value; }
        }

        private bool _isMonitor = true;
        /// <summary>
        /// 是否监视
        /// </summary>
        public bool IsMonitor
        {
            get { return this._isMonitor; }
            set { this._isMonitor = value; }
        }

        private string _arguments = string.Empty;
        /// <summary>
        /// 启动参数
        /// </summary>
        public string Arguments
        {
            get { return this._arguments; }
            set { this._arguments = value; }
        }

        private int _delayStartMilliseconds = 0;

        private int _delayStartTime = 0;
        /// <summary>
        /// 延迟启动时间(s)
        /// </summary>
        public int DelayStartTime
        {
            get { return this._delayStartTime; }
            set
            {
                this._delayStartTime = value;
                this._delayStartMilliseconds = value * 1000;
            }
        }

        private bool _enableHeartCheck = false;
        /// <summary>
        /// 是否启用心跳检测[true:启用;false:禁用]
        /// </summary>
        public bool EnableHeartCheck
        {
            get { return this._enableHeartCheck; }
            set { this._enableHeartCheck = value; }
        }

        private int _heartTimeoutMilliseconds = -1;
        private int _heartTimeoutSeconds = -1;
        /// <summary>
        /// 心跳超时时长
        /// </summary>
        public int HeartTimeoutSeconds
        {
            get { return this._heartTimeoutSeconds; }
            set
            {
                this._heartTimeoutSeconds = value;
                if (value > 0)
                {
                    this._heartTimeoutMilliseconds = value * 1000;
                }
                else
                {
                    this._heartTimeoutMilliseconds = WatchdogConfig.Instance.HeartCheckSecondsTimeout * 1000;
                }
            }
        }
        #endregion










        private bool _disposabled = false;
        public bool Disposabled
        {
            get { return this._disposabled; }
        }

        public void Dispose()
        {
            this.RemoveMonitor();
        }
    }
}
